var storeId = getQueryString("id");
$(function () {
    $("#jqGrid").Grid({
        url: '../storeuser/list?storeId=' + storeId,
        colModel: [
            {label: '用户姓名', name: 'nickName', index: "NICK_NAME", width: 75},
            {label: '所属部门', name: 'deptName', index: "DEPT_NAME", width: 75},
            {label: '邮箱', name: 'email', width: 90},
            {label: '手机号', name: 'mobile', width: 100},
            {
                label: '状态', name: 'status', width: 80, formatter: function (value) {
                    return value === 0 ?
                        '<span class="label label-danger">禁用</span>' :
                        '<span class="label label-success">正常</span>';
                }
            },
            {
                label: '特种人员', name: 'special', index: "SPECIAL", width: 80, formatter: function (value) {
                    return value === 0 ?
                        '<span class="label label-danger">否</span>' :
                        '<span class="label label-success">是</span>';
                }
            },
            {
                label: '创建时间', name: 'gmtCreated', width: 80,
                formatter: function (value) {
                    return transDate(value, "yyyy-MM-dd hh:mm");
                }
            },
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
        ]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        storeUser: {userId: '', storeId: '', id: ''},
        ruleValidate: [],
        q: {
            userName: ''
        },
        userList: []
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.storeUser = {userId: '', storeId: storeId, id: ''};
            vm.getDropDown();
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
            vm.getDropDown();
        },
        saveOrUpdate: function (event) {
            var url = vm.storeUser.id == (null || '') ? "../storeuser/save" : "../storeuser/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.storeUser),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../storeuser/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../storeuser/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.storeUser = r.storeUser;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'userName': vm.q.userName},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                userName: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        getDropDown: function () {
            Ajax.request({
                url: "../sys/user/queryAllRoleUser?roleCode=RL01",
                async: true,
                successCallback: function (r) {
                    vm.userList = r.list;
                }
            });
        }
    }
});